/*
 * MainServer.java
 *
 * Created on August 10, 2006, 10:59 PM
 *
 */
package org.letva.copie.mainserver;

import java.io.*;
import java.util.*;

import org.letva.copie.mainserver.gui.MainServerWindow;
import org.letva.copie.common.message.SubServerInfo;
import org.letva.copie.common.utils.Translations;

/**
 *
 * @author Vuk Nikolic
 */
public class MainServer {

	private SubServerListener subServerListener = null;
	private int subServerPort = 7772;
	private static MainServerProtocol myProtocol = null;
	private static int totalSubServerId = 0;

	private static Translations translations = new Translations("org/letva/copie/mainserver/translations");

	private PrintWriter log = null;
	private File logFile = null;
	private Map<String, SubServerInfo> subServers = new HashMap<String, SubServerInfo>();

	/** Creates a new instance of MainServer */
	public MainServer(MainServerWindow frame, int p, String fileName) {

		myWindow = frame;
		subServerPort = p;
		try {
			createLog(fileName);
			subServerListener = new SubServerListener(this);
			createProtocol();
			writeLog("Log started.");
		} catch (IOException e) {
			myWindow.setDialogException("Log File Error", "Error while creating log file");
		}

	}

	public MainServer() {
		this(null, 7772, "log.txt");
	}

	public void createLog(String fileName) throws IOException {
		logFile = new File(fileName);
		log = new PrintWriter(new FileWriter(logFile));
	}

	public Map<String, SubServerInfo> getSubServers() {
		return subServers;
	}

	public synchronized void addSubServer(SubServerInfo ssi) {
		subServers.put(ssi.getUrl(), ssi);
	}

	public synchronized void removeSubServer(SubServerInfo ssi) {
		subServers.remove(ssi.getUrl());
	}

	public int getSubServerPort() {
		return subServerPort;
	}

	public void setSubServerPort(int p) {
		subServerPort = p;
	}

	public void start() {
		subServerListener.start();
	}

	public void stop() {
		if (subServerListener != null) {
			subServerListener.stopSubServerListener();
		}
	}

	private void createProtocol() {
		if (myProtocol == null) {
			MainServer.myProtocol = new MainServerProtocol(this);
		}
	}

	public static MainServerProtocol getProtocol() {
		return myProtocol;
	}

	public LinkedList getThreads() {
		return subServerListener.getThreads();
	}
	
	public int getNextSubServerId() {
		return totalSubServerId++;
	}

	public MainServerWindow getMyWindow() {
		return myWindow;
	}
	MainServerWindow myWindow = null;
	private int activeSubServerThreads = 0;

	public synchronized void incActiveSubServerThreads() {
		activeSubServerThreads++;
		myWindow.setActiveSubServersLabel();
	}

	public synchronized void decActiveSubServerThreads() {
		activeSubServerThreads--;
		myWindow.setActiveSubServersLabel();
	}

	public int getActiveSubServerThreads() {
		return activeSubServerThreads;
	}

	public synchronized void writeLog(String s) {
		try {
			log = new PrintWriter(new FileWriter(logFile, true));
			log.println("[" + new Date() + "] " + s);
			log.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static String getLabel(String label) {
		return translations.getLabel(label);
	}

}

